package com.jerei.plugin.control;

import com.alibaba.fastjson.JSONArray;
import com.jerei.plugin.entity.WcmCommonRole;
import com.jerei.plugin.entity.WcmCommonRoleMenuDetail;
import com.jerei.plugin.service.IRoleMenuService;
import com.jerei.plugin.service.IWcmCommonRoleMenuDetailService;
import com.jerei.plugin.service.IWcmCommonRoleService;
import com.jerei.utils.UtilsCommon;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class WcmCommonRoleControl extends PluginController {

    @Autowired
    private IWcmCommonRoleService iWcmCommonRoleService;

    @Autowired
    private IRoleMenuService iRoleMenuService;

    @RequiresPermissions({"common-role:save", "common-role:update"})
    public String save() {
        WcmCommonRole entity = this.wrapper(WcmCommonRole.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonRoleService.update(entity);
        } else {
            iWcmCommonRoleService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-role:remove"})
    public String delete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonRoleService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }


    @RequiresPermissions({"common-role:save", "common-role:update", "common-role:view"})
    public void edit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonRole entity = null;
        if (null != id) {
            entity = iWcmCommonRoleService.find(id);
        } else {
            Long pid = UtilsWeb.getLong(this.getRequest(), "pid");
            entity = new WcmCommonRole();
            entity.setMenuId(1L);
            if (null != pid) {
                entity.setPid(pid);
            }
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-role:view"})
    public String devTreeAll() {
        Long menuNo = 1L;
        JSONArray array = iWcmCommonRoleService.allDevRoles();
        if (null == array) {
            return new JSONArray().toJSONString();
        }
        return array.toJSONString();
    }


    @RequiresPermissions({"common-role:view"})
    public String devTreeShow() {
        Long menuNo = 1L;
        JSONArray array = iWcmCommonRoleService.showDevRoles();
        if (null == array) {
            return new JSONArray().toJSONString();
        }
        return array.toJSONString();
    }

    @RequiresPermissions({"common-role:view"})
    public String groupTreeShow() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
        JSONArray array = iWcmCommonRoleService.showGroupRoles(gid);
        if (null == array) {
            return new JSONArray().toJSONString();
        }
        return array.toJSONString();
    }


    @RequiresPermissions({"common-menu:view"})
    public String devPageMenuTree() {
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        Long menuNo = 1L;
        if (null == roleId) {
            return new JSONArray().toJSONString();
        }
        return iRoleMenuService.treeDevMenu(roleId);
    }

    @RequiresPermissions({"common-menu:save", "common-menu:update"})
    public void addPower() {
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        String idStr = UtilsWeb.getString(this.getRequest(), "idStr");
        if (!"".equals(idStr)) {
            String[] idStrArr = idStr.split(",");
            Long[] idLongArr = new Long[idStrArr.length];
            for (int i = 0; i < idStrArr.length; i++) {
                idLongArr[i] = UtilsCommon.getLong(idStrArr[i]);
            }
            iRoleMenuService.addPowerForRole(roleId, idLongArr);
        }
    }

    @RequiresPermissions({"common-menu:remove"})
    public void deletePower() {
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        String idStr = UtilsWeb.getString(this.getRequest(), "idStr");
        if (!"".equals(idStr)) {
            String[] idStrArr = idStr.split(",");
            Long[] idLongArr = new Long[idStrArr.length];
            for (int i = 0; i < idStrArr.length; i++) {
                idLongArr[i] = UtilsCommon.getLong(idStrArr[i]);
            }
            iRoleMenuService.deletePowerForRole(roleId, idLongArr);
        }

    }

    @RequiresPermissions({"common-menu:remove"})
    public void clearPower() {
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        iRoleMenuService.clearPowerForRole(roleId);
    }

    @Autowired
    private IWcmCommonRoleMenuDetailService iWcmCommonRoleMenuDetailService;

    @RequiresPermissions({"common-menu:save", "common-menu:update", "common-menu:view"})
    public void editPower() {
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        Long menuDetailId = UtilsWeb.getLong(this.getRequest(), "menuDetailId");
        WcmCommonRoleMenuDetail obj = iWcmCommonRoleMenuDetailService.find(roleId, menuDetailId);
        this.getModel().addAttribute("obj", obj);
    }

    @RequiresPermissions({"common-menu:save", "common-menu:update"})
    public void saveMenuPower() {
        WcmCommonRoleMenuDetail wcmCommonRoleMenuDetail = this.wrapper(WcmCommonRoleMenuDetail.class);
        iWcmCommonRoleMenuDetailService.update(wcmCommonRoleMenuDetail);
    }
}